package com.zjh.webShell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zjh.common.config.SnowflakeConfig;
import com.zjh.common.entity.Response;
import com.zjh.common.exception.ServiceException;
import com.zjh.webShell.entity.CommonCommandsCategoryDO;
import com.zjh.webShell.service.ICommonCommandsCategoryService;
import com.zjh.webShell.service.ICommonCommandsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-23
 */
@RestController
@RequestMapping("/WebShell/common-commands-category")
public class CommonCommandsCategoryController {

    @Resource
    private SnowflakeConfig snowflakeConfig;

    @Resource
    private ICommonCommandsCategoryService commonCommandsCategoryService;

    @Resource
    private ICommonCommandsService commonCommandsService;

    @GetMapping("list")
    public Response<List<CommonCommandsCategoryDO>> list() {
        List<CommonCommandsCategoryDO> list = commonCommandsCategoryService.list();

        return Response.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Response<?> saveOrUpdate(@RequestBody CommonCommandsCategoryDO req) {

        String newName = req.getName();
        if (StringUtils.isBlank(newName)) {
            throw new ServiceException("新名称不能为空");
        }
        if (Objects.isNull(req.getPid())) {
            throw new ServiceException("父分类不能为空");
        }

        // 重命名校验, 同一个父节点下不能存在相同的分类
        QueryWrapper<CommonCommandsCategoryDO> queryWrapper0 = new QueryWrapper<>();
        queryWrapper0.eq("pid", req.getPid());
        queryWrapper0.eq("name", req.getName());
        List<CommonCommandsCategoryDO> exitList = commonCommandsCategoryService.list(queryWrapper0);
        if (CollectionUtils.isNotEmpty(exitList)) {
            throw new ServiceException("同父节点下分类名已经存在");
        }

        // 新增分类
        if (req.getId() == null) {
            req.setId(snowflakeConfig.snowflakeId());
            commonCommandsCategoryService.save(req);
        }
        // 重命名分类
        else {
            QueryWrapper<CommonCommandsCategoryDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", req.getId());
            commonCommandsCategoryService.update(req, queryWrapper);
        }

        return Response.success();
    }

    @PostMapping("delete")
    public Response<?> delete(@RequestBody CommonCommandsCategoryDO req) {
        Long id = req.getId();
        QueryWrapper<CommonCommandsCategoryDO> queryWrapper = new QueryWrapper<>();
        if (id != null) {
            queryWrapper.eq("id", id);
            commonCommandsCategoryService.remove(queryWrapper);
        } else {
            throw new ServiceException("缺少必要参数id");
        }

        return Response.success();
    }

}

